package com.shangxia.monomer.study.designPatterns.creationType.factory;

import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;

/**
 * @ProjectName: monomer
 * @ClassName: CalculatorDemo
 * @Time: 2021/7/29
 * @Author: zhouy
 * @Description:
 * @Experience:
 */

@Slf4j
public class CalculatorDemo {
    public static void main(String[] args) throws Exception {
        OperateBase operate = CalculatorFactory.getOperateBase("+");
        Integer result = operate.operate(2, 3);
        System.out.println(result);
    }
}

class CalculatorFactory {

    public static OperateBase getOperateBase(String operate) {
        return new OperateBase(operate);
    }

}

@AllArgsConstructor
class OperateBase {

    private String operate;

    public Integer operate(Integer numA, Integer numB) throws Exception {
        switch (operate) {
            case "+":
                return numA + numB;
            case "-":
                return numA - numB;
            case "*":
                return numA * numB;
            case "/":
                if (0 == numB) {
                    throw new Exception("0 不能做除数");
                }
                return numA / numB;
            default:
                throw new Exception("无此 " + operate + " 算法");
        }
    }

}